package cn.easier.activity.console.repository;

import cn.easier.activity.console.entity.AdvertisingPosition;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 广告位信息 Repository
 */
@Repository
public interface AdvertisingPositionRepository extends JpaRepository<AdvertisingPosition, Integer>, JpaSpecificationExecutor<AdvertisingPosition> {

    @Query(value = "select a.* from advertising_position a where ((:activityId is null or :activityId = '') or a.`activity_info_id` = :activityId) and a.`is_del` =0 order by a.id desc LIMIT :pageCount , :pageSize ", nativeQuery = true)
    List<AdvertisingPosition> findAllBy(@Param("activityId") Integer activityId, @Param("pageCount") int pageCount, @Param("pageSize") int pageSize);

    @Query(value = "select count(a.id) from advertising_position a where ((:activityId is null or :activityId = '') or a.`activity_info_id` = :activityId) and a.`is_del` =0 order by a.id ", nativeQuery = true)
    long findAllByCount(@Param("activityId") Integer activityId);

    @Query(value = "UPDATE `advertising_position` a SET a.is_del = ?  WHERE a.id = ? ", nativeQuery = true)
    @Modifying
    int delAdvertisingPosition(Integer status, Integer id);

    @Query(value = "select a.* from advertising_position a where a.`activity_info_id` =:activityInfoId and a.is_del = :isDel and a.top_flag = :topFlag ",nativeQuery = true)
    List<AdvertisingPosition> findBy(@Param("activityInfoId") Integer activityInfoId, @Param("isDel") int isDel, @Param("topFlag") int topFlag);
}
